package com.point.business.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.gson.JsonObject;
import com.point.business.database.managers.InformationManager;
import com.point.business.database.model.InformationND;
import com.point.business.parsers.ParserND;
import com.point.business.util.ParserException;
import com.point.business.util.UTFUtil;

/**
 * Servlet implementation class LoginServlet
 */

public class ParseNDServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ParseNDServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		UTFUtil.utf8(request, response);
		try {
			List<FileItem> multiparts = new ServletFileUpload(
					new DiskFileItemFactory()).parseRequest(request);
			FileItem DSTfile = (FileItem) multiparts.get(0);
			InputStream filecontent = DSTfile.getInputStream();
			ParserND ndData = new ParserND();
			List<InformationND> result = ndData.parse(filecontent);
			boolean isCreated = true;
			for (InformationND informationND : result) {
				isCreated &= InformationManager.getInstance().createNDRecords(
						informationND.getDocumentNumber(),
						informationND.getType(), informationND.getFullName(),
						informationND.getAddress(), informationND.getCity(),
						informationND.getAuthority(),
						informationND.getEvidationNumber(),
						informationND.getDocumentName(),
						informationND.getAnnouncmentIssueNumber(),
						informationND.getAnnouncmentIssueDate(),
						informationND.getAnounceDatum());
			}
			PrintWriter out = response.getWriter();
			JsonObject jsono = new JsonObject();
			if (isCreated) {
				jsono.addProperty("success", true);
				out.print(jsono);
			} else {
				jsono.addProperty("errorMsg", "Настана грешка!");
				out.print(jsono);
			}
		} catch (ParserException e) {
			JsonObject jsono = new JsonObject();
			PrintWriter out = response.getWriter();
			jsono.addProperty("errorMsg", e.getMessage());
			out.print(jsono);
			e.printStackTrace();
		} catch (FileUploadException e) {
			JsonObject jsono = new JsonObject();
			PrintWriter out = response.getWriter();
			jsono.addProperty("errorMsg", e.getMessage());
			out.print(jsono);
			e.printStackTrace();
		}

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
